/**
 * 
 */
package com.cqan.system.controller;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.context.SecurityContextHolder;

import com.cqan.account.entity.User;
import com.cqan.exception.AppException;

/**
 * @Description 通用上传模块
 * @author slaton.wu@gmail.com
 * @date 2012-7-23下午3:48:30
 * 
 */
public abstract class CommonUploadController {

	final String[] allowedSuffix = new String[] { ".jpg", ".png", ".gif",
			".jpeg", ".jpe", ".bmp", ".rar", "war", ".jar", ".zip", ".txt",
			".doc", ".xls", ".xlsx", ".docx", ".pptx", ".ppt", ".pdf" };

	/**
	 * 通用上传接口
	 * 
	 * @param request
	 * @return
	 * @throws AppException
	 */
	public abstract String upload(HttpServletRequest request)
			throws AppException;

	protected User getUserDetails() {
		Object obj = SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		if (obj instanceof User) {
			return (User) obj;
		}
		return null;
	}

	/**
	 * 按日期生成路径如：/2012/7/23/
	 * 
	 * @return
	 */
	public String createSavePath() {
		Calendar calendar = Calendar.getInstance();
		String path = new StringBuffer("/")
				.append(String.valueOf(calendar.get(Calendar.YEAR)))
				.append("/").append(calendar.get(Calendar.MONTH) + 1)
				.append("/").append(calendar.get(Calendar.DATE)).append("/")
				.toString();
		return path;
	}

}
